package com.example.pushup;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;


public class ExerciseActivity extends Activity{
	
	SensorManager mySensorManager;
	Sensor myLightSensor;
	TextView textLightSensor;
	TextView textPushupSet;
	TextView textPushupCount;
	TextView textLightSensorData;
	CountDownTimer countDown;
	
	private boolean initialized;
	private float 	data;
	private float 	lastData;
	private float 	initialValue;
	private int 	count;
	private int 	level;
	private int		rest;
	private	int[] 	routine;
	private	int 	set;
	private	long[] 	pattern = {1000, 500, 2000};
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.exercise);
		
		textLightSensor = (TextView)findViewById(R.id.lightsensor);
		textPushupSet	= (TextView)findViewById(R.id.pushupSet);
		textPushupCount = (TextView)findViewById(R.id.pushupCount);
		textLightSensorData = (TextView)findViewById(R.id.lightsensordata);//
		
		mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);       
		myLightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
		initialized=false;
		
		Intent intent = getIntent();  
		level = intent.getIntExtra("LEVEL", 1);
		rest= intent.getIntExtra("RESTTIME", 30);
		
		switch(level)
		{
		case 1:
			routine= new int[]{2, 3, 3, 3, 2};  break;
		case 2:
			routine= new int[]{3, 3, 4, 4, 3};  break;
		case 3:
			routine= new int[]{5, 4, 5, 4, 3};  break;
		case 4:
			routine= new int[]{8, 6, 6, 5, 4};  break;
		case 5:
			routine= new int[]{10, 8, 7, 6, 5}; break;
		}
			
		
		if (myLightSensor == null)      
			textLightSensor.setText("No Light Sensor!");
		else
			mySensorManager.registerListener(lightSensorEventListener, myLightSensor,SensorManager.SENSOR_DELAY_FASTEST);
		

		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	
	SensorEventListener lightSensorEventListener= new SensorEventListener()
	{

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onSensorChanged(SensorEvent event) {
			
			// TODO Auto-generated method stub
			
			if(event.sensor.getType()==Sensor.TYPE_LIGHT)
			{
				data= event.values[0];
				textLightSensorData.setText("Light Sensor Data:" + String.valueOf(data));
				
				if (!initialized)
				{
					initialValue= data;
					lastData= data;
					set=0;
					count= routine[set];
					set++;
					textPushupSet.setText( "[ "+String.valueOf(set) + " SET ]");
					textPushupCount.setText(String.valueOf(count));
					initialized = true;
				}
				else	
				{	
					
					if(lastData<initialValue*0.4 && data>initialValue*0.4)
					{
						count--;
						textPushupSet.setText( "[ "+String.valueOf(set) + " SET ]");
						textPushupCount.setText(String.valueOf(count));
						
						if(count==0 && set<5)
						{
						      countDown = new CountDownTimer(rest*1000,1000) {
						    	  
						    	  	   @Override
						               public void onTick(long millisUntilFinished) {
						 
						                  // TODO Auto-generated method stub
						    	  		  textPushupSet.setText( "Rest Time");
						                  textPushupCount.setText(String.valueOf(millisUntilFinished / 1000));
						               }

						               @Override 
						               public void onFinish() {
						 
						                  // TODO Auto-generated method stub  
						            	  count=routine[set];
										  set++;
										  
										  Vibrator Vi=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
										  Vi.vibrate(pattern, 2);
										  
										  if(set<=5)
											  Toast.makeText(getApplicationContext(), String.valueOf(set)+"세트 시작합니다.", 1000).show();
										  
										  textPushupSet.setText( "[ "+String.valueOf(set) + " SET ]");
										  textPushupCount.setText(String.valueOf(count));
						               }
						 
						       }.start();
						}
			
					}
					
					lastData= data;
					
					if(set==5 && count==0)
					{
						AlertDialog.Builder dlg = null;
						dlg = new AlertDialog.Builder( ExerciseActivity.this );
						dlg.setMessage("운동이 끝났습니다! 수고하셨습니다!");
						dlg.setTitle( "END" );
						
						dlg.setPositiveButton("확인", new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								
								Intent intent= new Intent(ExerciseActivity.this, MenuActivity.class);
								startActivity(intent);
							}
						});
						
						dlg.show(); 				

					}
					
				}
			}
			
		}};
		

}
